<template>
  <div class="equipmentLineChartBox">
    <div :id="id" :class="className" :style="{height:height,width:width}" />
  </div>
</template>

<script>
import resize from '@/components/Charts/mixins/resize'
import { updateLineChart } from '@/api/equipment-lineChart'
import echarts from 'echarts'
import { fetchEquipmentInfo } from '@/api/equipment-card'

export default {
  name: 'EquipmentLineChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '570px'
    },
    equipmentNumber: {
      type: String,
      default: 'bd16265284510910002'
    },
    date: {
      type: Array
    }
  },
  data() {
    return {
      listQuery: {
        date: this.date,
        equipmentNumber: this.equipmentNumber
      },
      i1data: '',
      i2data: '',
      i3data: '',
      i4data: '',
      equipmentName: '',
      chart: {}
    }
  },
  watch: {
    equipmentNumber() {
      this.listQuery.equipmentNumber = this.equipmentNumber
      fetchEquipmentInfo(this.listQuery.equipmentNumber).then((res) => {
        this.equipmentName = res.data.equipmentName
        setTimeout(() => {
        }, 1.5 * 1000)
        this.handleUpdateLineChart()
      })
    },
    date() {
      this.listQuery.date = this.date
      this.handleUpdateLineChart()
    }
  },
  mounted() {
    this.width = '100%'
    this.$nextTick(() => {
      fetchEquipmentInfo(this.listQuery.equipmentNumber).then((res) => {
        this.equipmentName = res.data.equipmentName
        setTimeout(() => {
        }, 1.5 * 1000)
      })
      this.handleUpdateLineChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    // 提交数据时间数据和设备编号得到相应数据
    handleUpdateLineChart() {
      console.log(this)
      updateLineChart(this.listQuery).then(response => {
        this.i1data = response.data.i1data
        this.i2data = response.data.i2data
        this.i3data = response.data.i3data
        this.i4data = response.data.i4data
        this.initChart()
      })
    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      window.onresize = this.chart.resize
      this.chart.setOption({
        title: {
          top: 20,
          text: this.equipmentName,
          textStyle: {
            fontWeight: 'normal',
            fontSize: 18,
            color: 'rgba(23, 221, 39, 1)'
          },
          left: '1%'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          top: 20,
          icon: 'rect',
          itemWidth: 20,
          itemHeight: 5,
          itemGap: 13,
          data: ['保护侧电位', '未保护侧电位', '南北地电位梯度', '东西地电位梯度'],
          right: '10%',
          textStyle: {
            fontSize: 20,
            color: '#F1F1F3'
          }
        },
        grid: {
          top: 100,
          left: '2%',
          right: '2%',
          bottom: '10%',
          containLabel: true
        },
        xAxis: [{
          type: 'time',
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              color: '#ffffff'
            }
          },
          axisLabel: {
            margin: 10,
            textStyle: {
              fontSize: 14,
              width: 1
            }
          },
          splitLine: {
            lineStyle: {
              color: '#57617B'
            }
          }
        }],
        yAxis: [{
          type: 'value',
          name: '电位数据',
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#ffffff'
            }
          },
          axisLabel: {
            formatter: '{value} V',
            margin: 10,
            textStyle: {
              fontSize: 14,
              width: 1
            },
            fontStyle: 'italic',
            fontWeight: 'bold'
          },
          splitLine: {
            lineStyle: {
              color: '#57617B'
            }
          }
        },
        {
          type: 'value',
          name: '梯度数据',
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#ffffff'
            }
          },
          axisLabel: {
            formatter: '{value} mV',
            margin: 10,
            textStyle: {
              fontSize: 14,
              width: 1
            },
            fontStyle: 'italic',
            fontWeight: 'bold'
          },
          splitLine: {
            lineStyle: {
              color: '#57617B'
            }
          }
        }],
        dataZoom: [{
          show: true,
          height: 25,
          xAxisIndex: [
            0
          ],
          bottom: 20,
          start: 75,
          end: 100,
          handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
          handleSize: '110%',
          handleStyle: {
            color: '#d3dee5'

          },
          textStyle: {
            color: '#fff' },
          borderColor: '#90979c'

        }, {
          type: 'inside',
          show: true,
          height: 15,
          start: 1,
          end: 35
        }],
        series: [{
          name: '保护侧电位',
          type: 'line',
          smooth: true,
          yAxisIndex: 0,
          symbol: 'circle',
          symbolSize: 5,
          showSymbol: false,
          lineStyle: {
            normal: {
              width: 2
            }
          },
          itemStyle: {
            normal: {
              color: 'rgb(171,246,2)',
              borderColor: 'rgba(137,189,2,0.27)',
              borderWidth: 12
            }
          },
          data: this.i1data
        }, {
          name: '未保护侧电位',
          type: 'line',
          yAxisIndex: 0,
          smooth: true,
          symbol: 'circle',
          symbolSize: 5,
          showSymbol: false,
          lineStyle: {
            normal: {
              width: 2
            }
          },
          itemStyle: {
            normal: {
              color: 'rgb(0,136,212)',
              borderColor: 'rgba(0,136,212,0.2)',
              borderWidth: 12

            }
          },
          data: this.i2data
        }, {
          name: '南北地电位梯度',
          type: 'line',
          yAxisIndex: 1,
          smooth: true,
          symbol: 'circle',
          symbolSize: 5,
          showSymbol: false,
          lineStyle: {
            normal: {
              width: 2
            }
          },
          areaStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                offset: 0,
                color: 'rgba(0, 36, 42, 0.3)'
              }, {
                offset: 0.8,
                color: 'rgba(0, 36, 42, 0)'
              }], false),
              shadowColor: 'rgba(0, 0, 0, 0.1)',
              shadowBlur: 10
            }
          },
          itemStyle: {
            normal: {
              color: 'rgb(252,206,56)',
              borderColor: 'rgba(0,36,42,0.2)',
              borderWidth: 12
            }
          },
          data: this.i3data
        }, {
          name: '东西地电位梯度',
          type: 'line',
          smooth: true,
          yAxisIndex: 1,
          symbol: 'circle',
          symbolSize: 5,
          showSymbol: false,
          lineStyle: {
            normal: {
              width: 2
            }
          },
          itemStyle: {
            normal: {
              color: 'rgb(219,50,51)',
              borderColor: 'rgba(219,50,51,0.2)',
              borderWidth: 12
            }
          },
          data: this.i4data
        }]
      })
    }
  }
}
</script>

<style>
.equipmentLineChartBox {
  box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
  transition: 0.3s;
  width: 100%;
  border-radius: 5px;
  background-color: #1b275e;
}
.equipmentLineChartBox:hover {
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
#tab-EquipmentLineChart {
  color: #b3efff;
}
#tab-EquipmentLineChart:hover {
  color: #fff;
}
#tab-EquipmentTable {
  color: #b3efff;
}
#tab-EquipmentTable:hover {
  color: #fff;
}
</style>
